*==============================================================================*
*         event study graphs for tier 1 exhaust standards, 1990-2000           *
*==============================================================================*


*------------------------- Panels A,B: exhaust standards ----------------------*
u "replicationFiles/dataSTATA/combined/combined_newcars.dta", clear 
keep if model_year >= 1990 & model_year <= 2000

keep if standard_CO < . & standard_HC < .
keep standard_* ldt model_year 
duplicates drop

* regression version of two groups
forv y = 1990/2000 {
	if `y' != 1993 g myear_`y'     = model_year == `y'
	if `y' != 1993 g ldtXmyear_`y' = ldt * myear_`y'
}

foreach p in CO HC {
	reg standard_`p' ldt ldtXmyear_* myear_* 

	cap drop x xb 
	g x = _n + 1989 in 1/11
	g xb    = 0 in 4
	forv y = 1990/2000 {
		loc n = `y' - 1989
		if `y' != 1993 replace xb    = _b[ldtXmyear_`y'] in `n'
	}
	tw (connected xb x in 1/11, mcolor(navy) lcolor(navy)), ///
		saving("results/figures/f4_evstud_tier1/standards_`p'.gph", replace) graphr(color(white)) ///
		yscale(noline) xline(1993 1996) xtit("Model Year") ///
		ytit("Emissions Rate, LDT - LDV") yline(0, lcolor(black)) ///
		legend(order(1 "Point estimate" 2 "95% C.I.")) xlab(1990 1993 1996 2000) ///
		subtit("")
	graph export "results/figures/f4_evstud_tier1/standards_`p'.wmf", replace
	graph export "$overleaf/figures/f4_evstud_tier1/standards_`p'.eps", replace
}



*-------------------------- Panel C,D: New vehicles ---------------------------*
u "replicationFiles/dataSTATA/combined/combined_newcars.dta", clear

keep if model_year >= 1990 & model_year <= 2000

* regression version of two groups
forv y = 1990/2000 {
	if `y' != 1993 g myear_`y'     = model_year == `y'
	if `y' != 1993 g ldtXmyear_`y' = ldt * myear_`y'
}

preserve
	drop myear_1994 ldtXmyear_1994 
	foreach p in CO HC {
		reg emissions_new_`p' ldt ldtXmyear_* myear_*, cluster(id_cluster)

		cap drop x xb ci_hi ci_lo
		g x = _n + 1989 in 1/11
		g xb    = 0 in 4
		g ci_hi = 0 in 4
		g ci_lo = 0 in 4
		forv y = 1990/2000 {
			loc n = `y' - 1989
			if `y' != 1993 & `y' != 1994 & `y' != 1995 ///
				replace xb    = _b[ldtXmyear_`y'] in `n'
			if `y' != 1993 & `y' != 1994 & `y' != 1995 ///
				replace ci_hi = _b[ldtXmyear_`y'] + 1.96*_se[ldtXmyear_`y'] in `n'
			if `y' != 1993 & `y' != 1994 & `y' != 1995 ///
				replace ci_lo = _b[ldtXmyear_`y'] - 1.96*_se[ldtXmyear_`y'] in `n'
		}
		tw (connected xb x, mcolor(navy) lcolor(navy)) ///
			(line ci_hi x, lpattern(dash) mcolor(cranberry) lcolor(cranberry)) ///
			(line ci_lo x, lpattern(dash) mcolor(cranberry) lcolor(cranberry)), ///
			saving("results/figures/f4_evstud_tier1/newcars_`p'.gph", replace) ///
			graphr(color(white)) yscale(noline) xline(1993 1996) ///
			xtit("Model Year") ytit("Emissions Rate, LDT - LDV") ///
			yline(0, lcolor(black)) legend(order(1 "Point estimate" 2 "95% C.I.")) ///
			xlab(1990 1993 1996 2000) subtit("")
			graph export "results/figures/f4_evstud_tier1/newcars_`p'.wmf", replace
			graph export "$overleaf/figures/f4_evstud_tier1/newcars_`p'.eps", replace
	}
restore





*----------------------- Panel E,F: Colorado smog check -----------------------*
u "dataSTATA/combined/combined_smogcheck_colorado.dta", clear

keep if model_year >= 1990 & model_year <= 2000

* regression version of two groups
forv y = 1990/2000 {
	if `y' != 1993 g myear_`y' = model_year == `y'
	if `y' != 1993 g ldtXmyear_`y' = ldt * myear_`y'
}


foreach p in CO HC {

	areg emissions_used_`p' ldt ldtXmyear_*, absorb(model_year) cluster(id_cluster)

	loc subtit "`p'"
	
	cap drop x xb ci_hi ci_lo
	g x = _n + 1989 in 1/11
	g xb    = 0 in 4
	g ci_hi = 0 in 4
	g ci_lo = 0 in 4
	forv y = 1990/2000 {
		loc n = `y' - 1989
		if `y' != 1993 replace xb    = _b[ldtXmyear_`y'] in `n'
		if `y' != 1993 replace ci_hi = _b[ldtXmyear_`y'] + 1.96*_se[ldtXmyear_`y'] in `n'
		if `y' != 1993 replace ci_lo = _b[ldtXmyear_`y'] - 1.96*_se[ldtXmyear_`y'] in `n'
	}
	tw (connected xb x in 1/11, mcolor(navy) lcolor(navy)) ///
		(line ci_hi x in 1/11, lpattern(dash) mcolor(cranberry) lcolor(cranberry)) ///
		(line ci_lo x in 1/11, lpattern(dash) mcolor(cranberry) lcolor(cranberry)), ///
		saving("results/figures/f4_evstud_tier1/usedcars_`p'.gph", replace) ///
		graphr(color(white)) yscale(noline) xline(1993 1996) ///
		xtit("Model Year") ytit("Emissions Rate, LDT - LDV") ///
		yline(0, lcolor(black)) legend(order(1 "Point estimate" 2 "95% C.I.")) ///
		xlab(1990 1993 1996 2000) subtit("")
		graph export "results/figures/f4_evstud_tier1/usedcars_`p'.wmf", replace
		graph export "$overleaf/figures/f4_evstud_tier1/usedcars_`p'.eps", replace
	sleep 2000
}



